﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Windows.Input;
using System.Windows.Media;
using VMwareEnabler.Model;

namespace VMwareEnabler.ViewModel
{
    /// <summary>
    /// This class contains properties that the main View can data bind to.
    /// <para>
    /// See http://www.galasoft.ch/mvvm
    /// </para>
    /// </summary>
    public partial class MainViewModel : ViewModelBase
    {
        private readonly IDataService _dataService;

        private readonly string nat = "VMware NAT Service";
        public string NAT { get { return nat; } }
        internal bool natenabled = false;
        public Brush NATEnabled { get { return natenabled ? Brushes.DarkGreen : Brushes.Red; } }

        private readonly string usb = "VMware USB Arbitration Service";
        public string USB { get { return usb; } }
        internal bool usbenabled = false;
        public Brush USBEnabled { get { return usbenabled ? Brushes.DarkGreen : Brushes.Red; } }

        private readonly string dhcp = "VMware DHCP Service";
        public string DHCP { get { return dhcp; } }
        internal bool dhcpenabled = false;
        public Brush DHCPEnabled { get { return dhcpenabled ? Brushes.DarkGreen : Brushes.Red; } }

        private readonly string auth = "VMware Authorization Service";
        public string AUTH { get { return auth; } }
        internal bool authenabled = false;
        public Brush AUTHEnabled { get { return authenabled ? Brushes.DarkGreen : Brushes.Red; } }

        private readonly string net1 = Network.Default.Network1;
        public string NET1 { get { return net1; } }
        internal bool net1enabled = false;
        public Brush NET1Enabled { get { return net1enabled ? Brushes.DarkGreen : Brushes.Red; } }

        private readonly string net2 = Network.Default.Network2;
        public string NET2 { get { return net2; } }
        internal bool net2enabled = false;
        public Brush NET2Enabled { get { return net2enabled ? Brushes.DarkGreen : Brushes.Red; } }

        private bool allenabled = false;

        public ICommand EnableButtonCommand { get; private set; }
        public ICommand LaunchVMwareCommand { get; private set; }
        public ICommand DisableButtonCommand { get; private set; }
        public ICommand CheckButtonCommand { get; private set; }

        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public MainViewModel(IDataService dataService)
        {
            _dataService = dataService;
            _dataService.GetData(
                (item, error) =>
                {
                    if (error != null)
                    {
                        // Report error here
                        return;
                    }
                });

            EnableButtonCommand = new RelayCommand(enable);
            LaunchVMwareCommand = new RelayCommand(launch);
            DisableButtonCommand = new RelayCommand(Disable);
            CheckButtonCommand = new RelayCommand(check);
            //Model.Model.Check();
        }

        private void enable()
        {
            Model.Model.Enable();
            Task t = new Task(() => { System.Threading.Thread.Sleep(2000); check(); });
            t.Start();
            allenabled = true;
        }

        private void launch()
        {
            if (this.allenabled == false)
            {
                Debug.WriteLine("True");
                enable();
            }
            Model.Model.Launch();
        }

        internal void Disable()
        {
            Model.Model.Disable();
            Task t = new Task(() => { System.Threading.Thread.Sleep(2000); check(); });
            t.Start();
            allenabled = false;
        }

        private void check()
        {
            this.allenabled = Model.Model.Check();
            RaisePropertyChanged("NATEnabled");
            RaisePropertyChanged("USBEnabled");
            RaisePropertyChanged("DHCPEnabled");
            RaisePropertyChanged("AUTHEnabled");

            RaisePropertyChanged("NET1Enabled");
            RaisePropertyChanged("NET2Enabled");
        }


        ////public override void Cleanup()
        ////{
        ////    // Clean up if needed

        ////    base.Cleanup();
        ////}
    }
}